package mybox.engine.astern;

import mybox.engine.Figur;
import mybox.engine.File4Qcad;

public class Gitter implements Figur{

  File4Qcad file4qcad;
  public int rows;
  public int columns;
  
  Knoten[][] instance;
  public static double SPACE=Knoten.RADIUS*4;
    
	public Gitter(File4Qcad file4qcad, int rows, int columns)  {
		this.rows=rows;
		this.columns=columns;
		this.file4qcad=file4qcad;
		instance=new Knoten[rows][columns];
		for (int rowIdx=0;rowIdx<rows;rowIdx++)
			for (int colIdx=0;colIdx<columns;colIdx++) 
				{
					Knoten newKnoten =new Knoten(file4qcad,rowIdx*SPACE,colIdx*SPACE);
					
					if (rowIdx>0) {
						
						newKnoten.nachbarn[Knoten.SOUTH]=instance[rowIdx-1][colIdx];
						instance[rowIdx-1][colIdx].nachbarn[Knoten.NORTH]=newKnoten;
						
						if (colIdx>0) {
							
							newKnoten.nachbarn[Knoten.WEST]=instance[rowIdx][colIdx-1];
							instance[rowIdx][colIdx-1].nachbarn[Knoten.EAST]=newKnoten;
							
							newKnoten.nachbarn[Knoten.SOUTHWEST]=instance[rowIdx-1][colIdx-1];
							instance[rowIdx-1][colIdx-1].nachbarn[Knoten.NORTHEAST]=newKnoten;							
						}
						
						if (colIdx<columns-1) {							
							newKnoten.nachbarn[Knoten.SOUTHEAST]=instance[rowIdx-1][colIdx+1];
							instance[rowIdx-1][colIdx+1].nachbarn[Knoten.NORTHWEST]=newKnoten;							
						}
						
					}
					
					instance[rowIdx][colIdx]=newKnoten;
					
				}
	}

	@Override
	public void draw() {
		for (int rowIdx=0;rowIdx<rows;rowIdx++)
			for (int colIdx=0;colIdx<columns;colIdx++)
				instance[rowIdx][colIdx].draw();		
	}
  
}
